package com.tomtop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.laolei.test.dao.UserMapper;
import com.laolei.test.model.User;
import com.tomtop.core.datasource.core.Proxy;

@Component
public class MasterSlaveSwapTest implements CommandLineRunner {
	@Autowired UserMapper mapper;
	@Override
	public void run(String... args) throws Exception {
		/**
		 * 使用注解切换
		 */
		User user =new User("66SS","2222");
		mapper.insert(user);
//		mapper.insert(user);
//		mapper.insert(user);
		System.err.println(user.toJSONString());
		mapper.selectAll().forEach(user0 -> System.err.println(user0.toJSONString()));
		Proxy.slave(() -> {
			mapper.findById().forEach(user0 -> System.err.println(user0.toJSONString()));
			return null;
		});
//		
//		System.out.println("本次调用将从主库查询");
//		currency = currencyService.findByIdOnMaster(1);
//		System.out.println(JSONUtils.toJSONString(currency));
//		
//		System.out.println("本次调用将切换到从库查询");
//		currency = currencyService.findByIdOnSalve(1);
//		System.out.println(JSONUtils.toJSONString(currency));
//		
//		/**
//		 * 使用代码切换
//		 */
//		System.out.println("本次调用将从主库查询");
//		Optional<Currency> pcurrency = Proxy.master(()->currencyService.findById(1) );
//		System.out.println(JSONUtils.toJSONString(pcurrency.get()));
//
//		System.out.println("本次调用将切换到从库查询");
//		pcurrency =Proxy.slave(()->currencyService.findById(1) );
//		pcurrency =Proxy.slave(()->currencyService.findById(1) );
//		System.out.println(JSONUtils.toJSONString(pcurrency.get()));
	}
}
